package pATT.DataSetsGenerator.dataSetsGenerator.generator.implement;

import java.util.Random;
import java.util.Vector;

import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Relation;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.IData;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.RandomWeightData;

public class RandomWeightGenerator extends Generator{
	
	public RandomWeightGenerator(Relation relat){
		super(relat);
	}
	
	private double random() {
		double doub;
		Random randomWord= new Random();
		doub=randomWord.nextDouble();
		return doub;
	}
	
	//Genera un valor para random distinto de valueWeight
	@SuppressWarnings("unchecked")
	public String randomValue (IData data){
		RandomWeightData d = (RandomWeightData)data; 
		Vector att = d.getAttribute().getValues();
		if(att.size() <= 1)
			return "";
		String val = (String) att.elementAt((int)(random()*(att.size())));
		while(val.equals(d.getValueWeight()))
			val = (String) att.elementAt((int)(random()*(att.size())));
		return val;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public Vector generate(IData data){
		int weight = ((RandomWeightData)data).getWeight();
		System.out.println("Con Peso: "+weight);	
		String value = ((RandomWeightData)data).getValueWeight();
		if(weight != -1){
			int totalRul = getRelation().getExamples();
			Vector att = ((RandomWeightData)data).getAttribute().getValues();
			Vector result = new Vector();
			Vector att1=new Vector();
			att1.addAll(att);
			double weightd = (double)weight / (double)100;
			double weightvalue = totalRul * weightd;
			int weightV = (int)weightvalue; 
			
			for (int i = 0; i < weightV; i++) {
				
				result.addElement(value);// agrego el valor con porcentaje
			}
			
			/*			int resto = totalRul - weightV;
			 Random randomWord= new Random();
			 att1.removeElement(value);
			 double doub;
			 
			 String val = "";
			 for (int i = 0; i < resto; i++) {
			 doub=randomWord.nextDouble();
			 val = (String) att1.elementAt((int)(doub*(att1.size())));//agrego demas valores sin "value"
			 result.addElement(val);
			 }
			 */			
			int resto = totalRul - weightV;
			String val = "##";
			for (int i = 0; i < resto; i++) {
				result.addElement(val);
			}
			
			Random randomWord= new Random();
			
			double dou;
			Vector newResult = new Vector();
			int num=0;
			int total=result.size();
			for (int i = 0; i < total; i++) {
				dou=randomWord.nextDouble();
				num=(int)(dou*(result.size()));
				val = (String) result.elementAt(num);//desordeno result para no dejar todos los "value" juntos
				newResult.addElement(val);
				result.removeElementAt(num);
				
			}
			System.out.println("--->newResult: "+newResult);
			return newResult;
		}
		else{
			System.out.println("------------->FUe al random comun");
			return (new RandomGenerator(relation)).generate(data);
		}
	}
}

